/**
 * [fcMessage]
 * @基于layui Message
 * @ver: 1.2
 * @date: 2022-3-10
 * @author: cm.yu
 */
layui.link('./js/lay-module/fcMessage/fcMessage2.css');
layui.define(['jquery'],function (exports) {
	var $ =  layui.$,
		messageFlag = true;

    var _b = 'fc-message-box',  //总消息存放盒子
        _m = 'message-notice',  //单个消息结构
        _fadeOutClass = 'notice-slide-fadeOut',
        _fadeInClass = 'notice-slide-fadeIn';

    var obj = {
    	open: function (info,options) {
    		var defaults = {
                target: 'body',
				duration:3000,		//显示时间, 毫秒。设为 0 则不会自动关闭
				showClose: false,	//是否显示关闭按钮
				position:'center',	//显示位置默认居中
                offset:'t',      //显示垂直居中，默认顶部
    		}
    		var opts = $.extend({},defaults,options);
    		
    		const icon = {
    			'info':'<svg t="1586399045233" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="4625" width="15" height="15"><path d="M522.318769 273.329231a58.919385 58.919385 0 1 0 58.919385-58.604308 57.186462 57.186462 0 0 0-58.919385 58.604308zM391.325538 476.790154a280.339692 280.339692 0 0 0 0 34.422154l51.357539-61.833846a40.014769 40.014769 0 0 1 29.223385-17.565539 13.312 13.312 0 0 1 7.876923 16.462769l-85.07077 281.757539a56.792615 56.792615 0 0 0 53.878154 72.625231 176.443077 176.443077 0 0 0 144.068923-102.4 277.740308 277.740308 0 0 0 0.945231-35.524924l-51.436308 61.912616a41.275077 41.275077 0 0 1-30.168615 17.565538 13.154462 13.154462 0 0 1-7.876923-14.887384l85.622154-283.175385a57.816615 57.816615 0 0 0-53.090462-72.861538 219.293538 219.293538 0 0 0-145.329231 103.502769zM512.393846 0a512 512 0 1 1-512 512 512 512 0 0 1 512-512z" fill="#ffffff" p-id="4626"></path></svg>',
    			'success':'<svg t="1586399279307" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="5668" width="15" height="15"><path d="M512 1024C229.2352 1024 0 794.7648 0 512S229.2352 0 512 0s512 229.2352 512 512-229.2352 512-512 512z m271.5264-653.7088l-54.2976-54.3104-307.7376 307.7376-126.72-126.72-54.2976 54.3104L385.28 696.128l36.1984 36.1984z" fill="#ffffff" p-id="5669"></path></svg>',
    			'error':'<svg t="1586398977558" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="3454" width="15" height="15"><path d="M512.345 2.207c-281.254 0-509.254 228-509.254 509.254s228 509.254 509.254 509.254 509.253-228 509.253-509.253-228-509.255-509.253-509.255z m223.847 663.93c19.628 19.672 19.628 51.58 0 71.252-9.813 9.836-22.676 14.76-35.538 14.76-12.861 0-25.724-4.925-35.539-14.76l-153.14-153.644-153.14 153.642c-9.814 9.836-22.677 14.76-35.532 14.76-12.869 0-25.724-4.925-35.538-14.76-19.628-19.672-19.628-51.58 0-71.253l153.14-153.648-153.141-153.641c-19.628-19.672-19.628-51.58 0-71.253 19.628-19.665 51.442-19.665 71.07 0l153.14 153.642 153.141-153.642c19.635-19.665 51.442-19.665 71.078 0 19.628 19.672 19.628 51.58 0 71.253L583.051 512.486l153.141 153.65z" fill="#ffffff" p-id="3455"></path></svg>',
    			'warning':'<svg t="1586399013003" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="4345" width="16" height="16"><path d="M0 0h1024v1024H0z" fill="#F8F8F8" fill-opacity="0" p-id="4346"></path><path d="M512 51.2a460.8 460.8 0 1 1 0 921.6 460.8 460.8 0 0 1 0-921.6zM438.272 281.0368l9.8304 246.3744c1.3824 34.304 29.5936 61.3888 63.8976 61.3888s62.464-27.136 63.744-61.44l9.216-246.272A73.5232 73.5232 0 0 0 511.488 204.8a73.3184 73.3184 0 0 0-73.216 76.2368zM435.2 741.888v0.512A76.8 76.8 0 0 0 512 819.2a76.288 76.288 0 0 0 76.288-76.8v-0.512A76.8 76.8 0 0 0 511.488 665.6a76.288 76.288 0 0 0-76.288 76.288z" fill="#ffffff" p-id="4347"></path></svg>',
    			'close':'<svg t="1618303899071" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="2025" width="18" height="18"><path d="M810.666667 273.493333L750.506667 213.333333 512 451.84 273.493333 213.333333 213.333333 273.493333 451.84 512 213.333333 750.506667 273.493333 810.666667 512 572.16 750.506667 810.666667 810.666667 750.506667 572.16 512z" p-id="2026" fill="#ffffff"></path></svg>',
    			'loading':'<svg viewBox="25 25 50 50" class="circular"><circle cx="50" cy="50" r="20" fill="none" class="path"></circle></svg>'
    		}

        	if(!messageFlag) return false; //如果loading层打开中，其他都不可点击

        	var close = '<em class="close">'+icon.close+'</em>';

            //单个消息元素
            if(opts.type == 'custom') {
                //自定义结构
                var _message = '<div class="'+_m+' '+_fadeInClass+' '+opts.type+'" position="'+opts.position+'"><div class="inner">'+info+ (opts.showClose?close:"")+'</div></div>';
            }else {
                var _message = '<div class="'+_m+' '+_fadeInClass+' '+opts.type+'" position="'+opts.position+'"><p><i>'+icon[opts.type]+'</i>'+ info + (opts.showClose?close:"")+'</p></div>';
            }

            // 消息位置显示分顶部和居中，分别会在不同结构中生成
            // 如果使用“target”参数指定了在某些结构中显示消息提示，而不是以body为目标，会自动进行区分
            if(opts.offset == 'c'){
                // 页面垂直居中
                if($('.'+_b+'[offset][target="'+opts.target+'"]').length < 1){
                    $(opts.target).append('<div class="'+_b+'"  offset="'+opts.offset+'" target="'+opts.target+'" '+(opts.target!="body"?"style='position:absolute;'":"")+'></div>');
                }
                var $dom = $(_message).appendTo( $('.'+_b+'[offset][target="'+opts.target+'"]'));
            }else if(opts.offset == 't') {
                // 靠页面顶部
                if($('.'+_b+'[target="'+opts.target+'"]:not([offset])').length < 1){
                    $(opts.target).append('<div class="'+_b+'" target="'+opts.target+'" '+(opts.target!="body"?"style='position:absolute;'":"")+'></div>');
                }
                var $dom = $(_message).appendTo( $('.'+_b+'[target="'+opts.target+'"]:not([offset])'));
            }else if(opts.offset == 'rb') {
                // 靠页面右下角
                if($('.'+_b+'[offset][target="'+opts.target+'"]').length < 1){
                    $(opts.target).append('<div class="'+_b+'"  offset="'+opts.offset+'" target="'+opts.target+'" '+(opts.target!="body"?"style='position:absolute;'":"")+'></div>');
                }
                var $dom = $(_message).appendTo( $('.'+_b+'[offset][target="'+opts.target+'"]'));
            }
            $dom.css({'height':$dom.height()})

            // cancel 只适用于loading
            if(opts.cancel) {
                this.cancel($dom,opts.cancel);
            }else{
                this.listen($dom,opts.duration,opts.onClose);
            }
    	},
    	listen: function (that,duration,cb) {
            if(duration > 0){
                var timer = setTimeout(function(){
                    obj.close(that,cb);
                },duration);
            }
            //点击关闭，触发手动关闭事件
            var $close = that.find('.close');
            $close.on('click',function(){
                var _that = $(this).parent().parent();
                obj.close(_that,cb);
                clearTimeout(timer)
            })
    	},
        close:function(that,cb) {//关闭后自动执行回调内容
            console.log(222)
            if(typeof(that) == 'object'){//每个元素有自己的关闭逻辑
                that.addClass(_fadeOutClass);
                that.removeClass(_fadeInClass);
                that.css({'height':0,'marginBottom':0});
                setTimeout(function(){
                    that.remove();
                    if(cb) return cb();
                },300)
            }else{//loading 自动关闭
                messageFlag = true;
                var _l = $('.'+_m+'.'+that);
                _l.addClass(_fadeOutClass);
                _l.removeClass(_fadeInClass);
                _l.css({'height':0,'marginBottom':0});
                setTimeout(function(){
                    _l.remove();
                    if(cb) return cb();
                },300)
            }
        },
        cancel:function(that,cb) {//不会自动关闭消息窗口，一般用于有ajax事件后，手动fcMessage.close()来关闭，配合loading来使用
            return cb();
        },
        info:function(info,opts) {
        	opts = $.extend(true,opts,{type:'info'})
        	this.open(info,opts);
        },
        success:function(info,opts) {
        	opts = $.extend(true,opts,{type:'success'})
        	this.open(info,opts);
        },
        warning:function(info,opts) {
        	opts = $.extend(true,opts,{type:'warning'})
        	this.open(info,opts);
        },
        error:function(info,opts) {
        	opts = $.extend(true,opts,{type:'error'})
        	this.open(info,opts);
        },
        loading:function(info,opts) {
        	opts = $.extend(true,opts,{type:'loading'})
        	this.open(info,opts);
            messageFlag = false;
        },
        custom:function(info,opts) {
            opts = $.extend(true,opts,{type:'custom'})
            this.open(info,opts);
        }
    };
	exports("fcMessage", obj);
});